package com.example.himalaya.model.dao;

import android.database.Cursor;
import android.media.MediaScannerConnection;
import android.os.Environment;
import android.provider.MediaStore;

import com.example.himalaya.base.BaseApplication;
import com.example.himalaya.view.callback.IDownloadedMusicDaoCallBack;
import com.example.himalaya.model.IDownLoadedMusicDao;
import com.ximalaya.ting.android.opensdk.model.PlayableModel;
import com.ximalaya.ting.android.opensdk.model.album.Announcer;
import com.ximalaya.ting.android.opensdk.model.track.Track;

import java.util.ArrayList;
import java.util.List;

public class DownloadedMusicDao implements IDownLoadedMusicDao {

    private List<Track> mListSong;
    private IDownloadedMusicDaoCallBack mCallBack;

    public DownloadedMusicDao() {

    }


    @Override
    public void getMusic() {
        MediaScannerConnection.scanFile(BaseApplication.getAppContext(), new String[]{Environment.getExternalStorageDirectory().getAbsolutePath()}, null, null);

        // 查询媒体库， 用获取的mp3信息， 填充到歌曲列表

        mListSong = new ArrayList<>();

        // search mp3 in phone
        Cursor musics = BaseApplication.getAppContext().getContentResolver().query(
                MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,
                new String[]{
                        MediaStore.Audio.Media._ID,
                        MediaStore.Audio.Media.DISPLAY_NAME,
                        MediaStore.Audio.Media.TITLE,
                        MediaStore.Audio.Media.DURATION,
                        MediaStore.Audio.Media.ARTIST,
                        MediaStore.Audio.Media.ALBUM,
                        MediaStore.Audio.Media.SIZE,
                        MediaStore.Audio.Media.DATA
                },
                MediaStore.Audio.Media.MIME_TYPE + "=?",
                new String[]

                        {
                                "audio/mpeg"
                        }, null);

        String fileName, title, singer, album, size, filePath;

        int duration;

        Track track;

        assert musics != null;
        if (musics.moveToFirst()) {
            do {
                fileName = musics.getString(1);
                title = musics.getString(2);
                duration = musics.getInt(3);
                singer = musics.getString(4);
                album = musics.getString(5);
                size = (musics.getString(6) == null) ? "未知" : musics.getInt(6) / 1024 / 1024 + "MB";
                if (musics.getString(7) != null) {
                    filePath = musics.getString(7);
                    track = new Track();

                    // 化成和喜马拉雅统一的秒
                    duration = duration / 1000;

                    track.setDuration(duration);
                    track.setDownloadedSaveFilePath(filePath);
                    track.setTrackTitle(title);
                    track.setKind(PlayableModel.KIND_TRACK);
                    track.setDataId(-1 * Math.abs(track.getTrackTitle().hashCode()));

                    Announcer announcer = new Announcer();
                    announcer.setNickname(singer);
                    track.setAnnouncer(announcer);


                    mListSong.add(track);
                }
            } while (musics.moveToNext());
        }
        musics.close();


        mCallBack.onMusicLoaded(mListSong);
    }

    @Override
    public void deleteMusic() {

    }

    @Override
    public void searchMusic() {

    }

    public void registerCallback(IDownloadedMusicDaoCallBack callback) {
        this.mCallBack = callback;
    }

    public void unRegisterCallBack() {
        mCallBack = null;
    }
}
